package com.cictec.middleware.gps.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.cictec.middleware.commons.cache.ecache.EHCacheUtils;
import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.ZoneUtils;
import com.cictec.middleware.core.entity.BusLine;
import com.cictec.middleware.core.entity.BusPark;
import com.cictec.middleware.core.entity.BusParkReturnNote;
import com.cictec.middleware.core.entity.pojo.ZoneFenceEntity;
import com.cictec.middleware.gps.service.feign.MiddlewareDcFeignClient;

/**
 * 场站管理（停车管理）service
 * 
 * @author ql
 *
 */
@Service
public class BusParkService {

	private static Logger logger = LoggerFactory.getLogger(BusParkService.class);

	
	
	@Autowired
	private MiddlewareDcFeignClient middlewareDcFeignClient;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	private BusLineService busLineService;

	private static String BUS_RETURN_PARK = "bus-return-park"; // 回场统计
	private static String BUS_RETURN_PARK_NOTE = "bus-return-park-note"; // 回场统计

	/**
	 * 判定车辆是否在场站内,如果在返回停车场信息
	 * @param busUuid
	 * @param nowLat
	 * @param nowLng
	 * @return
	 */
	public BusPark isInPark(String busUuid, String nowLat, String nowLng) {
		return isInPark(busUuid, Double.parseDouble(nowLat), Double.parseDouble(nowLng));
	}
	
	/**
	 * 判定车辆是否在场站内,如果在返回停车场信息
	 * 
	 * @param busUuid
	 * @param nowLat
	 * @param nowLng
	 * @return
	 */
	public BusPark isInPark(String busUuid, double nowLat, double nowLng) {

		// 回场信息判断
		List<BusPark> busParkList = getBusParkByBusUuid(busUuid);
		BusPark inBusPark = null;
		for (BusPark busPark : busParkList) {
			List<ZoneFenceEntity> zonePointList = busPark.getPoints();
			if (zonePointList.size() > 0) {
				boolean isInZone = ZoneUtils.isInZoneFence(zonePointList, nowLat, nowLng);
				if(isInZone){
					inBusPark = busPark;
					break;
				}
			}
		}
		return inBusPark;
	}
	
	/**
	 * 判定车辆是否该在场站内（是否应该回场）
	 * 根据车辆所在场站的线路（线路上有回场时间）
	 * 
	 * @param busUuid
	 * @return
	 */
	public boolean isShouldInPark(String busUuid, String lineUuid) {
		Date now = new Date();
		String today = DateUtils.getDate();
		boolean shouldInPark = false;
		List<BusPark> busParkList = getBusParkByBusUuid(busUuid);

		BusLine busLine = busLineService.getLineByLineUuid(lineUuid);

		String lineReturnParkNoteTime = busLine.getLineReturnParkNoteTime();

		if (!PMSUtils.isEmpty(lineReturnParkNoteTime)) {
			String[] lineReturnParkNoteTimeArr = lineReturnParkNoteTime.split("-");

			// 根据车辆uuid与时间 获取此时车辆是否需要回场校验
			if (busParkList.size() > 0) {
				Date startTime = DateUtils.parseDateTime(today + " " + lineReturnParkNoteTimeArr[0] + ":00");
				Date endTime = DateUtils.parseDateTime(today + " " + lineReturnParkNoteTimeArr[1] + ":00");

				if (startTime.getTime() < now.getTime() && now.getTime() < endTime.getTime()) {
					shouldInPark = true;
				}
			}
		}
		return shouldInPark;
	}

	
	
	/**
	 * 【缓存】查询场站
	 * @param busUuid
	 * @return
	 */
	public List<BusPark> getBusParkByBusUuid(String busUuid) {
		
		List<BusPark> parkList = EHCacheUtils.getValue(BUS_RETURN_PARK, busUuid);
		
		if(parkList != null	){
			return parkList;
		}
		
		parkList = selectBusParkByBusUuid(busUuid);
		
		EHCacheUtils.setValue(BUS_RETURN_PARK, busUuid, parkList);
		
		return parkList;
	}
	 
	/**
	 * 【数据库】查询场站
	 * 
	 * @param busUuid
	 * @return
	 */
	public List<BusPark> selectBusParkByBusUuid(String busUuid) {
		HashOperations<String, String, String> ops = stringRedisTemplate.opsForHash();
		String jsonParks = ops.get(BUS_RETURN_PARK, busUuid);
		if(PMSUtils.isNotEmpty(jsonParks)){
			List<BusPark> list = JSON.parseArray(jsonParks, BusPark.class);
			return list;
		}
		return new ArrayList<BusPark>();
	}
	
	/* ****************** 车辆回场统计 start **********************************************/
	
	/**
	 * 新增或修改车辆回场记录
	 * @param note
	 */
	public void saveBusParkReturnNote(BusParkReturnNote note){
		middlewareDcFeignClient.saveBusParkReturnNote(note);
	}
	
	
	/**
	 * 【缓存】获取回场信息
	 * @param date		日期
	 * @param busUuid	车辆UUID
	 * @param parkUuid	场站UUID
	 * @return
	 */
	public BusParkReturnNote getBusReturnParkNote(String date, String busUuid) {

		BusParkReturnNote note = EHCacheUtils.getValue(BUS_RETURN_PARK_NOTE, busUuid);

		if (note != null) {
			if(date.equals(note.getBrpDate())){
				return note;
			}
		}

		note = selectBusReturnParkNote(date, busUuid);

		if(note != null){
			EHCacheUtils.setValue(BUS_RETURN_PARK_NOTE, busUuid, note);
		}
		return note;
	}
	
	/**
	 * 【数据库】获取回场信息
	 * @param date		日期
	 * @param busUuid	车辆UUID
	 * @param parkUuid	场站UUID
	 * @return
	 */
	private BusParkReturnNote selectBusReturnParkNote(String date, String busUuid) {
		HashOperations<String, String, String> ops = stringRedisTemplate.opsForHash();
		String jsonParks = ops.get(BUS_RETURN_PARK_NOTE, busUuid);
		if(PMSUtils.isNotEmpty(jsonParks)){
			BusParkReturnNote note = JSON.parseObject(jsonParks, BusParkReturnNote.class);
			return note;
		}
		return null;
	}

	
	
	
	/* ****************** 车辆回场统计 end **********************************************/
}
